<?php

namespace app\admin\controller;

use app\admin\model\SchoolModel;
use cmf\controller\AdminBaseController;
use think\Db;
use think\db\Query;
use think\Validate;

class SchoolController extends AdminBaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $name=$this->request->param('name');
        $school=Db::name('school')
            ->join('cmf_area a','a.areaId=cmf_school.province','LEFT')
            ->join('cmf_area b','b.areaId=cmf_school.city','LEFT')
            ->join('cmf_area c','c.areaId=cmf_school.area','LEFT')
            ->where( function( Query $query) use ($name){
                if($name){
                    $query -> where ('name','like',"%$name%");
                }
            })
            ->field('cmf_school.*,a.areaName a,b.areaName b,c.areaName c')
            ->order('id desc')
            ->paginate('3',false,['query'=>$this->request->param()]);
        $page=$school->render();
        return $this->fetch('',['school'=>$school,'page'=>$page]);
    }

    /**
     * 显示添加资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function add()
    {
        $province=Db::name('area')
            ->where('level',1)
            ->select();
        return $this->fetch('school_add',['province'=>$province]);
    }
    public function addPost()
    {
        if(request()->isPost()){
            $date=input();
            $rule = [
               'name' => 'require',
               'province' => 'require',
               'city' => 'require',
               'area' => 'require'
            ];
            $msg = [
              'name.require' => '学校名称不能为空',
              'province.require' => '请选择省份',
              'city.require' => '请选择城市',
              'area.require' => '请选择县区'
            ];
            $validate = Validate::make($rule,$msg);
            $result = $validate->check($date);
            if(!$result){
                $this->error($validate->getError());
            }
            $add=SchoolModel::create($date);
            if($add){
                $this->success('添加成功','school/index');
            }else{
                $this->error('添加失败');
            }
        }
    }

    public function getCity()
    {
        $areaId = input('areaId');
        $city = Db::name('area')
            ->where('parentId',$areaId)
            ->where('level',2)
            ->select();
        //halt($city);
        $cityArr = [
            "<option value=''>--请选择城市--</option>"
        ];
        foreach ($city as $v){
            $opt="<option value='".$v['areaId']."'>".$v['areaName']."</option>";
            //halt($opt);
            array_push($cityArr,$opt);
        }
        return $cityArr;
    }

    public function getArea()
    {
        $cityId=input('cityId');
        //halt($cityId);
        $area=Db::name('area')
            ->where('parentId',$cityId)
            ->where('level',3)
            ->select();
        //halt($area);
        $cityArr = [
            "<option value=''>--请选择县区--</option>"
        ];
        foreach ($area as $v){
            $opt="<option value='".$v['areaId']."'>".$v['areaName']."</option>";
            //halt($opt);
            array_push($cityArr,$opt);
        }
        return $cityArr;
    }


    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $school = Db::name('school')->get($id);
        //halt($school);
        $area=Db::name('area')
            ->where('level',1)
            ->select();
        $city = Db::name('area')->where('level',2)->select();
        //halt($city);
        $areaA= Db::name('area')->where('level',3)->select();
        //halt($areaA);
        return $this->fetch('school_edit',[
            'school'=>$school,
            'area'=>$area,
            'city'=>$city,
            'areaA'=>$areaA
        ]);
    }
    public function editPost()
    {
        if(request()->isPost()){
            if(empty(input('name'))){
              $this->error('学校不能为空');  
            }
            $edit=Db::name('school') -> update(input());
            if($edit){
                $this->success('编辑成功','school/index');
            }else{
                $this->error('编辑失败');
            }
        }
    }


    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $del = Db::name('school')->where('id',$id)->delete();
        if($del){
            $this->success('删除成功','school/index');
        }else{
            $this->error('删除失败');
        }
    }
}
